package cn.hl.hm.quartz;

import cn.hl.hm.dao.OrderSeqMapper;
import cn.hl.hm.dao.RoadFeeMapper;
import cn.hl.hm.entity.OrderSeq;
import cn.hl.hm.entity.RoadFee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 
 * @author 大和
 *
 */
@Component
public class CloseOrderQuartz {
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	RoadFeeMapper roadFeeMapper;

	@Autowired
	private OrderSeqMapper om;
	private static final Logger logger = LoggerFactory.getLogger(PicReverseNumQuartz.class);

//	@Scheduled(cron = "0 */2 * * * ?") //每2分钟
	public void addRVP() {
		logger.info("【配载费-定时关闭10分钟未支付订单】   start______________________________");

		RoadFee roadFee = new RoadFee();
		roadFee.setPageId(0);
		roadFee.setPageCount(1000);
		List<RoadFee> selectNewOrder = roadFeeMapper.selectCloseOrder(roadFee);
		int i = 0;
		List<String> list = new ArrayList<>();
		if(selectNewOrder!=null&&selectNewOrder.size()>0){
			for (RoadFee roadFee2 : selectNewOrder) {
				if(roadFee2.getId()!=null){
					roadFee2.setOrderStatus("3");//关闭
					int res = roadFeeMapper.updateByPrimaryKeySelective(roadFee2);
					if(res>0){
						//累计序列号
						OrderSeq os = new OrderSeq();
						OrderSeq selectOrderSeqNum = om.selectOrderSeqNum(os);
						if(selectOrderSeqNum!=null){
							OrderSeq os2 = new OrderSeq();
							os2.setSeqNum(selectOrderSeqNum.getSeqNum()+1);
							// 添加订单成功，将seqNum保存至 order_seq 表中
							os2.setCreateTime(sdf.format(new Date()));
							int insertSelective = om.insertSelective(os2);		
							if(insertSelective>0){
								i++;
								
							}
						}
					}else {
						list.add(roadFee2.getOrderCode());
					}

				}
			}
			
		}
		logger.info("【配载费-定时关闭10分钟未支付订单】   本次共查出订单（"+selectNewOrder.size()+"）单，共关闭成功（"+i+"）单");
		if(list!=null&&list.size()>0){
			for (String string : list) {
				logger.info("【配载费-定时关闭10分钟未支付订单】  更新失败单号为 ："+string+"");

			}
		}

		logger.info("【配载费-定时关闭10分钟未支付订单】   end________________________________");
		
	}
}
